from tkinter import *
import re
from tkinter import filedialog
import time
import subprocess


class crontabtool():
    
    def __init__(self):
        pass

    #启动程序路径
    def processpath(self):
        self.path = filedialog.askopenfilenames()
        self.var1.set(self.path)

    def windowsserver(self,root):
        w = Toplevel(root)
        w.title('定时任务工具，测试使用，七七编写')
        w.geometry('550x200')
        w.geometry('+600+400')

        #标签框
        l1 = Label(w,text='时间:',font='12')
        l1.grid(row=1,column=0,padx=5,pady=5)

        l2 = Label(w,text='程序:',font='12')
        l2.grid(row=2,column=0,padx=5,pady=5)

        l3 = Label(w,text='格式如：12:35',font='12',bg='yellow')
        l3.grid(row=1,column=2,padx=5,pady=5)


        #输入框
        self.e1 = Entry(w)
        self.e1.grid(row=1,column=1,padx=5,pady=5)

        self.var1=StringVar()
        self.e22 = Entry(w,textvariable=self.var1)
        self.e22.grid(row=2,column=1,padx=5,pady=5)

        #按键
        b1 = Button(w,text='开始',font='12',bg='red',command=server.getresult)
        b1.grid(row=3,column=0,padx=5,pady=5)

        b2 = Button(w,text='选择程序',font='12',bg='yellow',command=server.processpath)
        b2.grid(row=2,column=2,padx=5,pady=5)

        w.mainloop()

    def getresult(self):
        while True:
            nowhour=time.strftime('%H',time.localtime())
            nowmin=time.strftime('%M',time.localtime())
            gettime= self.e1.get()
            gethour = re.split('\:',gettime)[0]
            getmin = re.split('\:',gettime)[1]
            print(nowhour,nowmin,self.path)
            if nowhour == gethour and nowmin == getmin:
                subprocess.Popen(self.path)
                time.sleep(5)
                break
            time.sleep(30)



#类实例化
server = crontabtool()

#单独执行此文件时，将#号去掉即可
#server.windowsserver()